package com.petRescueCenter.demo.posting.entity.dto;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Join;
import javax.persistence.criteria.JoinType;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.apache.commons.lang3.StringUtils;
import org.springframework.data.jpa.domain.Specification;

import com.petRescueCenter.demo.posting.entity.Reply;
import com.petRescueCenter.demo.user.entity.User;

import io.swagger.annotations.ApiModelProperty;

public class ReplyQueryDTO {
	@ApiModelProperty(name = "id", value = "评论id", required = false)
	private Long id;
	@ApiModelProperty(name = "postingId", value = "帖子id", required = false)
	private Long postingId;
	@ApiModelProperty(name = "parentReplyUserId", value = "被评论的用户id", required = false)
	private Long parentReplyUserId;
	private Long userId;
	private String userName;

	public String getUserName() {
		return userName;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}

	public Long getUserId() {
		return userId;
	}

	public void setUserId(Long userId) {
		this.userId = userId;
	}

	public Long getId() {
		return id;
	}

	public Long getPostingId() {
		return postingId;
	}

	public Long getParentReplyUserId() {
		return parentReplyUserId;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public void setPostingId(Long postingId) {
		this.postingId = postingId;
	}

	public void setParentReplyUserId(Long parentReplyUserId) {
		this.parentReplyUserId = parentReplyUserId;
	}

	@SuppressWarnings({ "serial" })
	public static Specification<Reply> getWhereClause(final ReplyQueryDTO replyQueryDTO) {
		return new Specification<Reply>() {
			@Override
			public Predicate toPredicate(Root<Reply> root, CriteriaQuery<?> query, CriteriaBuilder criteriaBuilder) {
				List<Predicate> predicate = new ArrayList<>();

				if (null != replyQueryDTO.getId()) {
					predicate.add(criteriaBuilder.equal(root.get("id").as(Long.class), replyQueryDTO.getId()));
				}
				if (null != replyQueryDTO.getUserId()) {
					Join<Reply, User> join = root.join("user", JoinType.LEFT);
					predicate.add(criteriaBuilder.equal(join.get("id").as(Long.class), replyQueryDTO.getUserId()));
				}
				if (null != replyQueryDTO.getPostingId()) {
					predicate.add(
							criteriaBuilder.equal(root.get("postingId").as(Long.class), replyQueryDTO.getPostingId()));
				}
				if (StringUtils.isNotBlank(replyQueryDTO.getUserName())) {
					List<Predicate> ps = new ArrayList<Predicate>();
					Join<Reply, User> join = root.join("user", JoinType.LEFT);
					ps.add(criteriaBuilder.like(join.get("name").as(String.class),
							'%' + replyQueryDTO.getUserName() + '%'));
					ps.add(criteriaBuilder.like(root.get("content").as(String.class),
							'%' + replyQueryDTO.getUserName() + '%'));
					predicate.add(criteriaBuilder.or(ps.toArray(new Predicate[ps.size()])));
				}
				if (null != replyQueryDTO.getParentReplyUserId()) {
					Join<Reply, Reply> join = root.join("parentReply", JoinType.LEFT);
					predicate.add(criteriaBuilder.equal(join.get("user").get("id").as(Long.class),
							replyQueryDTO.getParentReplyUserId()));
				}
				Predicate[] pre = new Predicate[predicate.size()];
				return query.where(predicate.toArray(pre)).getRestriction();
			}
		};
	}

}
